<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2020/6/17
 * Time: 20:28
 */
namespace App\Admin\Controllers;

use App\Admin\Traits\UserTrait;
use App\Helper;
use App\Http\Controllers\Controller;
use App\Lib\Sdk\Manager;
use App\Models\Forms\SystemConfig;
use App\Models\GroupConfig;
use App\Models\Robots;
use App\Models\System;
use Encore\Admin\Controllers\AdminController;
use Encore\Admin\Form;
use Encore\Admin\Layout\Content;
use Encore\Admin\Show;
use Illuminate\Http\Request;
use function Symfony\Component\String\u;

/**
 * 工具
 * Class ToolsController
 * @package App\Admin\Controllers
 */
class SystemController extends Controller
{

    use UserTrait;

    public function index(Content $content)
    {
        $uid = $this->getUserId();
       /* $model = System::getConfig();
        if (!$model) {
            $model = new System();
            $model->setAttribute("user_id", $uid);
            $model->save();
        }*/
        $model = new SystemConfig();
        return $content
            ->title("系统管理")
            ->body($this->form($model));

    }

    protected function form($model)
    {
        $form = new Form($model);
        $form->setTitle("部分系统配置仅允许使用配置文件修改");

        Helper::formLoadModel($form, $model, true);
        $form->disableCreatingCheck();
        $form->disableEditingCheck();
        $form->disableViewCheck();

        $model2 = System::getConfig();
        Helper::formLoadModel($form, $model2);
        $form->setAction("/admin/system?id=" . $model2->id);

        return $form;
    }

    public function store(Request $request)
    {
        $data = $request->post();
        /** @var System $model */
        $model = System::getConfig();

        if ($data['robot_interval'] > 0) {
            Robots::query()->update(['interval' => $data['robot_interval']]);
            GroupConfig::query()->update(['interval' => $data['robot_interval']]);
        }
        if ($data['max_wx_groups'] > 0) {
            Robots::query()->update(['group_max' => $data['max_wx_groups']]);
        }
        if ($model) {
            $model->update($data);
        }
        return back();
    }



}